/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import control.MStockMidlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import listener.ICommandListener;
import listener.IKeyListener;

/**
 *
 * @author tho.hoang
 */
public class Detail extends ACanvas {

    private DetailItemCollections items = new DetailItemCollections();
    private int selectedIndex = 0;
    private int itemY = 0, itemX = 0, moveStep = 20, itemsHeight;
    boolean painted = false, moveUp = false, moveDown = false;

    public Detail() {
    }

    public Detail(Display display, MStockMidlet control, String title, Image background, Image icon) {
        super(display, control, title, background, icon);
    }

    public void paintItems(Graphics g) {
        setItemsCoordinate();
        int y = itemY;
        ADetailItem item = null;
        if (items != null && items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                item = (ADetailItem) items.elementAt(i);
                item.paint(itemX, y, g);
                y += item.getHeight();
            }
        }
        painted = true;
    }

    public void addItem(ADetailItem item) {
        items.addElement(item, this);
    }

    public void removeItem(ADetailItem item) {
        items.removeElement(item);
    }

    public int getItemsSize() {
        return items.size();
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public ADetailItem getSelectedItem() {
        if (selectedIndex >= 0 && selectedIndex < items.size()) {
            return items.elementAt(selectedIndex);
        }
        return null;
    }

    private void setItemsCoordinate() {
        if (!painted) {
            moveStep = getFont().getFontNormal().getHeight();
            itemX = getItemX() + getStyle().getBodyStyle().getPadding().getLeft();
            itemY = getItemY() + getStyle().getBodyStyle().getPadding().getTop();
            oldIndex = 0;
            newIndex = 0;
            selectedIndex = 0;
            items.elementAt(selectedIndex).setFocus(true);
        }
        int x = itemX, y = itemY;
        itemsHeight = 0;
        ADetailItem item = null;
        for (int i = 0; i < items.size(); i++) {
            item = items.elementAt(i);
            item.setCoordinate(x, y);
            y += item.getHeight();
            itemsHeight += item.getHeight();
        }
        itemsHeight += getStyle().getBodyStyle().getPadding().getTopAndBottom();
        if (!painted) {
            if (itemsHeight > getItemHeight() - getStyle().getBodyStyle().getPadding().getTopAndBottom()) {
                moveUp = false;
                moveDown = true;
            } else {
                moveUp = false;
                moveDown = false;
            }
        }
    }

    private void nextMoveProcessing(int y) {
        if (items.size() == 0) {
            moveUp = false;
            moveDown = false;
        }

        if (y < getItemY() + getStyle().getBodyStyle().getPadding().getTop()) {
            moveUp = false;
        }
        if (y > itemsHeight) {
            moveDown = false;
        }
        if (y + itemsHeight < getItemHeight() + getItemY() + getStyle().getBodyStyle().getPadding().getTopAndBottom()) {
            moveDown = false;
            itemY = getItemHeight() - itemsHeight + getItemY();
        } else {
            moveDown = true;
        }
        if (y > getItemY()) {
            itemY = getItemY();
            moveUp = false;
        } else {
            moveUp = true;
        }
    }

    public void keyPressed(int key) {
        ICommandListener commandListener = getCommandListener();

        if (key == getLeftSoftKey()) {
            if (commandListener != null && getSelectCommand() != null) {
                commandListener.commandAction(getSelectCommand());
            }

        } else if (key == getRightSoftKey()) {
            if (commandListener != null && getCancelCommand() != null) {
                commandListener.commandAction(getCancelCommand());
            }
        } else {
            IKeyListener keyListener = getKeyListener();
            switch (key) {
                case KEY_NUM0:
                case KEY_NUM1:
                case KEY_NUM2:
                case KEY_NUM3:
                case KEY_NUM4:
                case KEY_NUM5:
                case KEY_NUM6:
                case KEY_NUM7:
                case KEY_NUM8:
                case KEY_NUM9:
                case KEY_STAR:
                case KEY_POUND:
                case FIRE:
                    if (keyListener != null) {
                        keyListener.keyAction(key);
                    }
                    break;
                default:
                    int gameAction = getGameAction(key);
                    if (gameAction == UP || gameAction == LEFT) {
                        boolean isRepaint = false;
                        if (moveUp) {
                            itemY += moveStep;
                            moveAction = MOVE_UP;
                            nextMoveProcessing(itemY);
                            isRepaint = true;
                        }

                        int i = 0, maxY, minY;
                        int paintHeight = getItemY() + getItemHeight() - getStyle().getBodyStyle().getPadding().getTopAndBottom();
                        ADetailItem item = null;

                        moveAction = MOVE_UP;
                        oldIndex = selectedIndex;
                        if (0 <= selectedIndex && selectedIndex < items.size()) {
                            item = items.elementAt(selectedIndex);
                            maxY = item.getY() + item.getHeight() + moveStep;
                            minY = item.getY() + moveStep;
                            if ((maxY > paintHeight) || (minY < getItemY())) {
                                ((ADetailItem) items.elementAt(selectedIndex)).setFocus(false);
                                selectedIndex = -1;
                            }
                        }

                        for (i = oldIndex - 1; i >= 0; i--) {
                            item = items.elementAt(i);
                            maxY = item.getY() + item.getHeight() + moveStep;
                            minY = item.getY() + moveStep;
                            if (((item instanceof LinkItem) || (item instanceof RankingItem)) && (maxY <= paintHeight) && (minY >= getItemY())) {
                                newIndex = i;
                                selectedIndex = i;
                                if (oldIndex >= 0) {
                                    ((ADetailItem) items.elementAt(oldIndex)).setFocus(false);
                                }
                                ((ADetailItem) items.elementAt(newIndex)).setFocus(true);
                                isRepaint = true;
                                break;
                            }
                        }

                        if (isRepaint && isAutoRepaint()) {
                            repaint();
                        }

                    } else if (gameAction == DOWN || gameAction == RIGHT) {
                        boolean isRepaint = false;
                        if (moveDown) {
                            itemY -= moveStep;
                            nextMoveProcessing(itemY);
                            isRepaint = true;
                        }

                        int i = 0, maxY, minY;
                        int paintHeight = getItemY() + getItemHeight() - getStyle().getBodyStyle().getPadding().getTopAndBottom();
                        ADetailItem item = null;

                        moveAction = MOVE_DOWN;
                        oldIndex = selectedIndex;
                        if (0 <= selectedIndex && selectedIndex < items.size()) {
                            item = items.elementAt(selectedIndex);
                            maxY = item.getY() + item.getHeight() - moveStep;
                            minY = item.getY() - moveStep;
                            if ((maxY > paintHeight) || (minY < getItemY())) {
                                ((ADetailItem) items.elementAt(selectedIndex)).setFocus(false);
                                selectedIndex = -1;
                            }
                        }

                        for (i = oldIndex + 1; i < items.size(); i++) {
                            item = items.elementAt(i);
                            maxY = item.getY() + item.getHeight() - moveStep;
                            minY = item.getY() - moveStep;
                            if (((item instanceof LinkItem) || (item instanceof RankingItem)) && (maxY <= paintHeight) && (minY >= getItemY())) {
                                newIndex = i;
                                selectedIndex = i;
                                if (oldIndex >= 0) {
                                    ((ADetailItem) items.elementAt(oldIndex)).setFocus(false);
                                }
                                ((ADetailItem) items.elementAt(newIndex)).setFocus(true);
                                isRepaint = true;
                                break;
                            }
                        }

                        if (isRepaint && isAutoRepaint()) {
                            repaint();
                        }

                    } else if (gameAction == FIRE) {
                    }

                    if (keyListener != null) {
                        keyListener.keyAction(gameAction);
                    }
                    break;
            }
        }
    }
}
